$(function() {
	var tree = $("#areaTree");
	var setting = {
		check: {
			enable: true
		},
		async: {
			enable: true,
			url: rootPath + "/area/listAreaListForSelector",
			autoParam: ["id", "name"]
		},
		data : {
			simpleData : {
				enable : true,
				idKey : 'id',
				pIdKey : 'parentId',
				rootPId : "1"
			},
			key: {
				name: "areaName"
			}
		},
		callback : {
			onClick : function onClick(e, treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj('areaTree')
				var nodes = zTree.getSelectedNodes();
				for (var i = 0, l = nodes.length; i < l; i++) {
					alert("ok");
				}
			}
		}
	};
	var nodes = [{"id":1,"areaName":"客户所属区域","parentId":0,"isParent":true, "path":"|"}];
	var treeObj = $.fn.zTree.init(tree, setting, nodes);

	// 添加所选区域到已选中列表
	$("#btn_add_all").click(function(){
		var zTree = $.fn.zTree.getZTreeObj('areaTree')
		var selectedNodes = [];
		var nodes = zTree.getCheckedNodes();
		for (var i = 0, l = nodes.length; i < l; i++) {
			var nodeObj = nodes[i];
			if(nodeObj.path.length == 22){
				var data = {"id":nodeObj.id,"areaName":nodeObj.areaName, "path":nodeObj.path};
				selectedNodes.push(data);
				// alert(JSON.stringify(data));
			}
		}
		if(selectedNodes.length > 0 ){
			window.parent.window.clearGetTargetList();
			for(var i = 0; i < selectedNodes.length; i++ ){
				window.parent.window.addAreaToSelectedTable(selectedNodes[i]);
			}
			parent.layer.closeAll();
		}else{
			layer.msg("请选择区（县）级数据",{time:1000});
		}
	});
	
});
